package com.sweet.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sweet.Result;
import com.sweet.dto.CommonDto;
import com.sweet.dto.OrderDto;
import com.sweet.mapper.OrderMapper;
import com.sweet.model.Order;
import io.micrometer.core.instrument.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class OrderController {
    @Resource
    private OrderMapper orderMapper;

    @Value("${server.port}")
    private String port;

    @GetMapping("/orders")
    public Result getList(CommonDto dto) {
        if (dto.getCurPage() == null) {
            dto.setCurPage(1);
        }
        if (dto.getPageSize() == null) {
            dto.setPageSize(10);
        }
        Page<Order> orderPage = orderMapper.selectPage(
                new Page<>(dto.getCurPage(), dto.getPageSize()), null);
        Map<String, Object> data = new HashMap<>();
        data.put("port",port);
        data.put("data", orderPage);
        return Result.ok(data);
    }

    @PostMapping("/orders")
    public Result getList(@RequestBody OrderDto dto) {
        Order order = new Order();
        BeanUtils.copyProperties(dto, order);
        orderMapper.insert(order);
        return Result.ok(port);
    }

    @GetMapping(value = "/payment/lb")
    public String getPaymentLB(){
        return port;
    }

    @GetMapping("/payment/zipkin")
    public String paymentZipkin()
    {
        return "hi ,i'am paymentzipkin server fall back，welcome to sweet，O(∩_∩)O哈哈~";
    }

}
